import 'dart:typed_data';

import 'package:dartx/dartx.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/errors.dart';
import 'package:sgy_flutter/ble/ext.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/gen/assets.gen.dart';
import 'package:sgy_flutter/model/device_type.dart';
import 'package:sgy_flutter/widget/toast.dart';

class AdjustZeroLogic extends GetxController {
  var adjusting = false.obs;

  var alert = "".obs;
  var image = "".obs;

  @override
  void onInit() {
    switch (bleManager.deviceModel?.deviceType) {
      case null:
        break;
      case DeviceType.dlsL:
      case DeviceType.dlsR:
        alert('请确保曲柄垂直地面，双脚离开踏板。'.tr);
        image(Assets.image.iconDlsLdjz.path);
        break;
      case DeviceType.hiflow:
      case DeviceType.axo:
      case DeviceType.axocc:
      case DeviceType.apm2:
        alert('请双脚离开踏板。'.tr);
        image("");
        break;
      case DeviceType.axpower:
        alert('请确保曲柄垂直地面，双脚离开踏板。'.tr);
        image(Assets.image.iconAxpLdjz.path);
        break;
      case DeviceType.jtL:
      case DeviceType.jrR:
      case DeviceType.unknow:
        break;
    }
    super.onInit();
  }

  Future<bool> adjustZero() async {
    adjusting(true);
    try {
      if (await bleManager.ensureDeviceConnected()) {
        if (bleManager.deviceModel?.isDoubleSlide == true ||
            rightBleManager.deviceModel?.isDoubleSlide == true) {
          Uint8List? leftData;
          Uint8List? rightData;
          try {
            leftData = await bleManager.adjustZero();
          } catch (e) {}
          try {
            rightData = await rightBleManager.adjustZero();
          } catch (e) {}
          if (leftData != null && rightData != null){
             if (leftData.contentEquals([0xfe, 0xff]) == true || rightData.contentEquals([0xfe, 0xff]) == true ) {
              showToast("充电中校准失败".tr);
              return false;
            } else if (leftData.contentEquals([0xfd, 0xff]) || rightData.contentEquals([0xfd, 0xff])) {
              showToast("没有垂直校准失败".tr);
              return false;
            } else if (leftData?.contentEquals([0xff, 0xff]) == true || rightData?.contentEquals([0xff, 0xff]) == true) {
               showToast("校准失败".tr);
               return false;
             } else{
              return true;
            }
          }else if (leftData == null && rightData == null){
            bleManager.ensureDeviceConnected();
            return false;
          }else{
            var data = leftData ?? rightData ?? [];
            if (data.contentEquals([0xff, 0xff]) == true) {
              showToast("校准失败".tr);
              return false;
            } else if (data.contentEquals([0xfe, 0xff])) {
              showToast("充电中校准失败".tr);
              return false;
            } else if (data.contentEquals([0xfd, 0xff])) {
              showToast("没有垂直校准失败".tr);
              return false;
            } else {
              return true;
            }
          }


        } else {
          final data = await bleManager.adjustZero();
          printError(info: "校准:${data.toHex()}");
          if (data.contentEquals([0xff, 0xff])) {
            showToast("校准失败".tr);
            return false;
          } else if (data.contentEquals([0xfe, 0xff])) {
            showToast("充电中校准失败".tr);
            return false;
          } else if (data.contentEquals([0xfd, 0xff])) {
            showToast("没有垂直校准失败".tr);
            return false;
          } else {
            return true;
          }
        }
      } else {
        return false;
      }
    } on EmptyWriteCharacteristicError catch (e) {
      showToast("请先移除充电线并重新连接".tr);
      return false;
    } catch (e) {
      return false;
    } finally {
      adjusting(false);
    }
  }
}
